<?php

namespace api\modules\v1\forms\clinic;

use Yii;
use yii\base\Model;
use common\models\member\Member;
use common\models\clinic\Project;

/**
 * 治疗项目
 *
 * Class ProjectForm
 * @package api\modules\v1\forms\clinic
 */
class ProjectForm extends Model
{
    /**
     * 治疗项目ID
     *
     * @var
     */
    public $id;

    /**
     * 治疗项目名
     *
     * @var
     */
    public $name;

    /**
     * 治疗项目名
     *
     * @var
     */
    public $bind_yiyuan_name;

    /**
     * 项目ID
     *
     * @var
     */
    public $project_id;

    /**
     * 治疗项目状态
     *
     * @var
     */
    public $status;

    /**
     * 模板
     *
     * @var
     */
    public $template;

    /**
     * 治疗项目信息
     *
     * @var
     */
    protected $clinicProject;

    /**
     * 排序
     *
     * @var
     */
    public $sort;

    public function rules()
    {
        return [
            // 参数验证
            [['project_id', 'name', 'bind_yiyuan_name', 'status', 'template'], 'required', 'message' => '{attribute}不能为空', 'on' => ['create']],
            [['project_id', 'id', 'name', 'bind_yiyuan_name', 'status', 'template'], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit']],
            [['project_id', 'id'], 'required', 'message' => '{attribute}不能为空', 'on' => ['delete']],
            [['project_id', 'id', 'status'], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit_status']],

            [['project_id'], 'checkProjectId', 'skipOnEmpty' => false, 'on' => ['create', 'edit', 'delete', 'edit_status']],
            [['id'], 'checkClinicProject', 'skipOnEmpty' => false, 'on' => ['edit', 'delete', 'edit_status']],
            [['name'], 'checkName', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
            [['bind_yiyuan_name'], 'checkBindYiyuanName', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
            [['status'], 'checkStatus', 'skipOnEmpty' => false, 'on' => ['create', 'edit', 'edit_status']],
            [['template'], 'checkTemplate', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();

        // 新增
        $scenarios['create'] = ['project_id', 'name', 'bind_yiyuan_name', 'status', 'template', 'sort'];

        // 编辑
        $scenarios['edit'] = ['project_id', 'id', 'name', 'bind_yiyuan_name', 'status', 'template', 'sort'];

        // 删除
        $scenarios['delete'] = ['project_id', 'id'];

        // 切换部门状态
        $scenarios['edit_status'] = ['project_id', 'id', 'status'];

        return $scenarios;
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'id' => '治疗项目ID',
            'name' => '治疗项目名称',
            'bind_yiyuan_name' => '奕远系统治疗项目名称',
            'project_id' => '项目ID',
            'status' => '状态',
            'template' => '模板类型',
            'sort' => '排序',
        ];
    }

    /**
     * 检验项目ID
     *
     * @param $attribute
     * @return bool
     */
    public function checkProjectId($attribute)
    {
        if (Yii::$app->user->identity->member->is_super == Member::IS_SUPER_YES) {
            return true;
        }

        $attr = Yii::$app->services->attr->findInfo([
            'member_id' => Yii::$app->user->identity->member_id,
            'project_id' => $this->project_id,
        ]);

        if (!$attr) {
            $this->addError($attribute, '未绑定当前项目，无法操作');
            return true;
        }
    }

    /**
     * 检验治疗项目信息
     *
     * @param $attribute
     * @return bool
     */
    public function checkClinicProject($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        $clinicProject = $this->getClinicProject();
        if (!$clinicProject) {
            $this->addError($attribute, '参数错误[id_error]');
            return true;
        }

        if ($clinicProject->status == Project::STATUS_DELETE) {
            $this->addError($attribute, '治疗项目已删除，无法操作');
            return true;
        }

//        if (($this->getScenario() == 'edit' && $this->status == Project::STATUS_INVALID)
//            || $this->getScenario() == 'delete'
//            || ($this->getScenario() == 'edit_status' && $this->status == Project::STATUS_INVALID)
//        ) {
//            $clinicProjectBind = Yii::$app->services->clinicProjectBind->findInfo(['clinic_project_id' => $this->id]);
//
//            if ($clinicProjectBind) {
//                $this->addError($attribute, "该治疗项目已绑定治疗室，无法操作");
//                return true;
//            }
//        }
    }

    /**
     * 校验治疗项目名称
     *
     * @param $attribute
     */
    public function checkName($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        $len = mb_strlen($this->name);
        if ($len > 50) {
            $this->addError($attribute, '治疗项目名称过长，最多50个字');
            return true;
        }

        // 是否检验
        $isCheck = true;
        if ($this->getScenario() == 'edit') {
            $clinicProject = $this->getClinicProject();
            // 治疗项目名称未变【跳过】
            if ($clinicProject->name == $this->name) {
                $isCheck = false;
            }
        }

        if ($isCheck) {
            $clinicProjectInfo = Yii::$app->services->clinicProject->findInfo([
                'project_id' => $this->project_id,
                'name' => $this->name,
                'status' => [Project::STATUS_INVALID, Project::STATUS_ENABLE],
            ]);

            if ($clinicProjectInfo) {
                $this->addError($attribute, "治疗项目【{$this->name}】已存在");
                return true;
            }
        }
    }

    /**
     * 校验奕远系统治疗项目名称
     *
     * @param $attribute
     */
    public function checkBindYiyuanName($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        $len = mb_strlen($this->bind_yiyuan_name);
        if ($len > 100) {
            $this->addError($attribute, '奕远系统治疗项目名称过长，最多100个字');
            return true;
        }
    }

    /**
     * 校验治疗项目状态
     *
     * @param $attribute
     */
    public function checkStatus($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        if (!in_array($this->status, [Project::STATUS_INVALID, Project::STATUS_ENABLE])) {
            $this->addError($attribute, "参数错误[status_error]");
            return true;
        }
    }

    /**
     * 校验治疗项目模板类型
     *
     * @param $attribute
     */
    public function checkTemplate($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        if (!in_array($this->template, array_keys(Project::$templates))) {
            $this->addError($attribute, "参数错误[template_error]");
            return true;
        }
    }

    /**
     * 获取治疗项目信息
     *
     * @return Project|null
     */
    public function getClinicProject()
    {
        if (empty($this->clinicProject)) {
            $this->clinicProject = Yii::$app->services->clinicProject->findById($this->id);
        }

        return $this->clinicProject;
    }
}